package cn.piesat.scanning.business.xz.service.gseimpl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.piesat.scanning.business.xz.service.BuildAlgorithmTaskService;
import cn.piesat.scanning.business.xz.single.AlgorithmTaskPathParam;
import cn.piesat.scanning.business.xz.single.AlgorithmTaskRuleParam;
import cn.piesat.scanning.dto.HtScanningMetadataDTO;
import cn.piesat.scanning.service.HtScanningMetadataService;
import cn.piesat.scanning.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Ming
 * @version 1.0
 * @description: TODO
 * @date 2023/12/18 13:48
 */
@Service
public class BuildTibet_CIAServiceImpl implements BuildAlgorithmTaskService {

    @Autowired
    private HtScanningMetadataService htScanningMetadataService;
    @Override
    public Map<String, Object> initAlgorithmParams(String adsType, String primaryFile, String filePath, AlgorithmTaskRuleParam taskRuleParam, AlgorithmTaskPathParam taskPathParam, Map<String, Integer> indexParams, String fileDate, String scanningPath,HtScanningMetadataDTO htScanningMetadataDTO) {
        Map<String, Object> algorithmParams = new HashMap<>();
        //天擎_气象要素_插值
        //SURF_CHN_MUL_DAY_20220101000000.json
        String fileName = filePath.substring(filePath.lastIndexOf("/") + 1);
        String[] s = fileName.split("_");
        String dateTime = s[4].substring(0,s[4].lastIndexOf("."));
        Map<String, Object> params = new HashMap<>();
        String startTime = dateTime;
        String endTime = dateTime;
        if (taskPathParam.getParameter04().contains("TD")) {
            if ("10".equals(startTime.substring(6, 8))) {
                startTime = startTime.substring(0, 6) + "01" + startTime.substring(8);
            } else if ("20".equals(startTime.substring(6, 8))) {
                startTime = startTime.substring(0, 6) + "11" + startTime.substring(8);
            } else if (Integer.parseInt(startTime.substring(6, 8)) >= 28) {
                startTime = startTime.substring(0, 6) + "21" + startTime.substring(8);
            }
        } else if (taskPathParam.getParameter04().contains("AM")) {
            String date = dateTime.substring(0, 6);
            startTime = date + "01000000";
            endTime = date + "31235959";
        } else if (taskPathParam.getParameter04().contains("AQ")) {
            String month = dateTime.substring(4, 6); // 假设月份信息在日期字符串的第5位和第6位
            String quarter = null;
            switch (month) {
                case "03":
                case "04":
                case "05":
                    quarter = "1";
                    break;
                case "06":
                case "07":
                case "08":
                    quarter = "2";
                    break;
                case "09":
                case "10":
                case "11":
                    quarter = "3";
                    break;
                case "12":
                case "01":
                case "02":
                    quarter = "4";
                    break;
            }
            String year = dateTime.substring(0, 4);

            if ("1".equals(quarter)) {
                startTime = year + "0301000000";
                endTime = year + "0531235959";
            } else if ("2".equals(quarter)) {
                startTime = year + "0601000000";
                endTime = year + "0831235959";
            } else if ("3".equals(quarter)) {
                startTime = year + "0901000000";
                endTime = year + "1131235959";
            } else if ("4".equals(quarter)) {
                startTime = year + "12000000";
                int i = Integer.parseInt(year);
                int nextYear = i + 1;
                endTime = nextYear + "0231235959";
            }
        } else if (taskPathParam.getParameter04().contains("AY")) {
            //BAS_NDVI_540000_L3_FY3D_MERSI_1000M_GLL_MOAD_20230701000000_20230701235959.TIFF
            String date = dateTime.substring(0, 4);
            startTime = date + "0101000000";
            endTime = date + "1231235959";
        }
        List<HtScanningMetadataDTO> metadataDTOList = htScanningMetadataService.findQxSite(startTime, endTime, s[3], "FY3D",
                s[0].replace("-", ""), s[1],  "JSON");
        List<String> collect = metadataDTOList.stream().map(x -> scanningPath + File.separator + x.getFilePath()).collect(Collectors.toList());
        String join = StrUtil.join(",", collect);
        if (metadataDTOList.size() == 0) {
            algorithmParams.put("parameter01", null);
        } else {
            params.put("primaryFile", join);
            params.put("auxPath", taskPathParam.getParameter03());
            params.put("CType", taskPathParam.getParameter04());
            params.put("ProdID", taskPathParam.getParameter05());
            algorithmParams.put("parameter00", adsType);
            algorithmParams.put("parameter01", params);
        }
        return algorithmParams;
    }
}
